package com.wk.service;

import com.wk.dao.CommentDao;
import com.wk.model.Msg;
import com.wk.vo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {
    @Autowired(required = false)
    private CommentDao commentDao;

    public boolean selectUserId(int bookId, int userId){
        Integer userid = commentDao.selectUserId(bookId, userId);
//        System.out.println(comment);
        return null != userid;
    }

    public boolean selectComment(int bookId, int userId){
        String comment = commentDao.selectComment(bookId, userId);
//        System.out.println(comment);
        return null != comment;
    }

    public boolean selectLast(int bookId, int userId){
        String lastTime = commentDao.selectLast(bookId, userId);
        return null != lastTime;
    }

    public String getMyComment(int bookId, int userId) {
        return commentDao.selectComment(bookId, userId);
    }

    public Msg insertComment(int bookId, int userId, String comment){
        try {
            commentDao.insertComment(bookId, userId, comment);
            return new Msg("200", "评论添加成功！", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg("400", "评论添加失败！", "");
        }
    }

    public Msg updateComment(int bookId, int userId, String comment){
        try {
            commentDao.updateComment(bookId, userId, comment);
            return new Msg("200", "评论更新成功！", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg("400", "评论更新失败！", "");
        }
    }

    public Msg insertAcquaintance(int bookId, int userId, String acquaintance){
        try {
            commentDao.insertAcquaintance(bookId, userId, acquaintance);
            return new Msg("200", "心得添加成功！", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg("400", "心得添加失败！", "");
        }
    }

    public Msg updateAcquaintance(int bookId, int userId, String acquaintance){
        try {
            commentDao.updateAcquaintance(bookId, userId, acquaintance);
            return new Msg("200", "心得更新成功！", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg("400", "心得更新失败！", "");
        }
    }

    public Msg insertLastInfo(int bookId, int userId, String lastTime, int lastLocation){
        try {
            commentDao.insertLastInfo(bookId, userId, lastTime, lastLocation);
            return new Msg("200", "日志添加成功！", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg("400", "日志添加失败！", "");
        }
    }

    public Msg updateLastInfo(int bookId, int userId, String lastTime, int lastLocation){
        try {
            commentDao.updateLastInfo(bookId, userId, lastTime, lastLocation);
            return new Msg("200", "日志更新成功！", "");
        } catch (Exception e) {
            e.printStackTrace();
            return new Msg("400", "日志更新失败！", "");
        }
    }

    public List<Comment> getAllComments(int bookId){
        List<Comment> comments = new ArrayList<>();
        try {
            comments = commentDao.getAllComments(bookId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return comments;
    }
}
